local scene = gamestate.new()
local editor = {}
local blockSize = 16

local tileType = {"森林","砖墙","混凝","水域","冰面","空白"}
function editor:init()
	self.mode = "draw"
	self.mapW = 26
	self.mapH = 26
	self.world = bump.newWorld()
	self.map = Map(self,self.mapW,self.mapH,blockSize)
	self:initCam()
    self.current = 1
    self.save_btn = {text = "保存"}
    self.load_btn = {text = "读取"}
    self.move_btn = {text = "移动"}
    self.reset_btn = {text = "重置"}

    self.save_input = {text = "",enable  = false,mono = true}
    for i,t in ipairs(tileType) do
    	self[t] = {text = t}
    end

    self.load_btns = {}
end
local sw,sh = love.graphics.getDimensions()
function editor:initCam()
	self.cam = Cam.new(0,0,1000*blockSize,1000*blockSize)
    self.cam:setWindow(0,30,sw*GlobalScale,sh * GlobalScale-30)
end

local x = 0
local y = 0
local w = 70
local h = 30
local off = 2

function editor:update()
	love.graphics.setFont(texture.midfont)
	local offx = 0
    local b = self.save_btn
    local r = suit.Button(b.text,b,x+offx,y,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
    	self.save_input.enable = true
        self.cam:setWindow(0,60,sw*GlobalScale,sh * GlobalScale-60)
    end

    if self.save_input.enable then
    	local b = self.save_input
	   	suit.Label("输入文件名：",1,32,150,30)
	    local r = suit.Input(b,b,150,32,200,30)
	    if r.hit then
	    	love.keyboard.setTextInput( true, 200/GlobalScale,32/GlobalScale,200/GlobalScale,30/GlobalScale)
	    elseif r.submitted then
	    	self:save(b.text)
	    	self.save_input.enable = false
	    	love.keyboard.setTextInput( false)
	    	self.cam:setWindow(0,30,sw*GlobalScale,sh * GlobalScale-30)
	    end
    end


    offx = offx + off + w

    local b = self.load_btn
    local r = suit.Button(b.text,b,x+offx,y,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        self:load()
        for i,v in ipairs(self.maps) do
        	self.load_btns[i] = {text = v.name}
        end
   		if self.maps[1] then
   			self.cam:setWindow(0,60,sw*GlobalScale,sh * GlobalScale-60)
   		end
    end

    if self.load_btns[1] then
	   	suit.Label("选择文件：",1,32,100,30)
	   	for i,v in ipairs(self.maps) do
        	local b = self.load_btns[i]
		    local r = suit.Button(b.text,b,80+i*60,32,60,30)
		    if r.entered then
		        sound:play("hover")
		    end
		    if r.hit then
		    	self:setMap(i)
		    	self.load_btns = {}
		    	self.cam:setWindow(0,30,sw*GlobalScale,sh * GlobalScale-30)
		    	break
		    end
        end
    end

    offx = offx + off+ w

    local b = self.move_btn
    local r = suit.Button(b.text,b,x+offx,y,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        self.mode = "move"
    end
    offx = offx + off+ w

    local b = self.reset_btn
    local r = suit.Button(b.text,b,x+offx,y,w,h)
    if r.entered then
        sound:play("hover")
    end
    if r.hit then
        editor.map.w = 26
		editor.map.h = 26
		editor.map:clear()
    end
    offx = offx + off+ w


 	for i,t in ipairs(tileType) do
    	local b = self[t]
	    local r = suit.Button(b.text,b,x+offx,y,w,h)
	    if r.entered then
	        sound:play("hover")
	    end
	    if r.hit then
	        self.mode = "draw"
	        self.current = i
	    end
	    offx = offx + off+ w
    end

	local d = love.keyboard.isDown
	for i = 1,5 do
		if d(tostring(i)) then
			self.current = i
		end
	end

	if d("left") then
		self.cam:move(-10,0)
	elseif d("right") then
		self.cam:move(10,0)
	end

	if d("up") then
		self.cam:move(0,-10)
	elseif d("down") then
		self.cam:move(0,10)
	end

	self.mx,self.my = self.cam:toWorld(love.mouse.getPosition())
	self.ix,self.iy = math.ceil(self.mx/blockSize),math.ceil(self.my/blockSize)

	if love.mouse.isDown(1) and self.mode == "draw" then
		if self.ix>self.map.w then 
			for x = self.map.w + 1,self.ix do
				for y = 1,self.map.h do
					self.map:clearTile(x,y)
				end
			end
			self.map.w = self.ix
			self.map:makeBorder()
			self.map:canvasExpand()
		end
		if self.iy>self.map.h then
			for y = self.map.h + 1,self.iy do
				for x = 1,self.map.w do
					self.map:clearTile(x,y)
				end
			end
			self.map.h = self.iy
			self.map:makeBorder()
			self.map:canvasExpand()
		end
		self.map:setTile(self.ix,self.iy,self.current)
	elseif love.mouse.isDown(2) and self.mode == "draw" then
		self.map:clearTile(self.ix,self.iy)
	end
end

function editor:draw()
	self.cam:draw(function()
        self.map:draw()
        love.graphics.setColor(1, 0, 1, 1)
        love.graphics.print(self.ix..","..self.iy, (self.ix-1)*blockSize, (self.iy-2)*blockSize)
        love.graphics.rectangle("line", (self.ix-1)*blockSize, (self.iy-1)*blockSize, blockSize, blockSize)
        love.graphics.setColor(1, 1, 1, 0.5)
    	texture:drawTile(self.current,(self.ix-1)*blockSize,(self.iy-1)*blockSize,0,2,2)
    end)
    
end

function editor:save(name)
	if not name or name == "" then return end
	if not love.filesystem.getInfo("usermap") then
		love.filesystem.createDirectory("usermap")
	end
	name = "usermap/"..name..".dat"
	local str_data = bitser.dumps(self.map:getData())
	love.filesystem.write(name,str_data)
end

function editor:load(i)
	self.maps = {}
	for i,filename in ipairs(love.filesystem.getDirectoryItems("/usermap/")) do
        if filename:sub(-4,-1) == ".dat" then
        	self.maps[i] = {
        		name = filename:sub(1,-5), 
        		data = bitser.loads(love.filesystem.read("/usermap/"..filename))}
        end
    end
end

function editor:setMap(i)
	self.map:setData(self.maps[i].data)
end
function scene:enter(from,screenshot,how,cx,cy)
	editor:init()
end

function scene:update(dt)
   editor:update()
end

function scene:draw()
	editor:draw()
	suit.draw()
end

function scene:keypressed(k)
	if k == "f1" then
		editor.map.w = 30
		editor.map.h = 30
		editor.map:clear()
	elseif k == "f2" then
		editor.map:random()
	elseif k == "f3" then
		editor.hide = not editor.hide
	elseif k == "escape" then
		editor:save()
		gamestate.switch(gameState.title)
	end
end

function scene:mousemoved(x, y, dx, dy, istouch)
	if editor.mode == "move" and love.mouse.isDown(1) then
		editor.cam:move(-dx,-dy)
	end
end

return scene